package com.tengroup.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tengroup.entity.Question;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface QuestionMapper extends BaseMapper<Question> {

    // 1. 插入题目
    // 继承自 BaseMapper，无需额外实现
    // int insert(Question question);

    // 2. 根据 ID 删除题目（硬删除）
    // 继承自 BaseMapper，无需额外实现
    // int deleteById(Long id);

    // 3. 根据 ID 软删除题目（设置 del_flag 为 1）
    @Update("UPDATE questions SET del_flag = 1 WHERE id = #{id}")
    int softDeleteById(@Param("id") Long id);

    // 4. 根据 ID 更新题目
    // 继承自 BaseMapper，无需额外实现
    // int updateById(Question question);

    // 5. 根据 ID 查询题目
    // 继承自 BaseMapper，无需额外实现
    // Question selectById(Long id);

    // 6. 查询所有未删除的题目
    @Select("SELECT * FROM questions WHERE del_flag = 0")
    List<Question> findAllQuestions();

    // 7. 分页查询所有未删除的题目
    @Select("SELECT * FROM questions WHERE del_flag = 0")
    IPage<Question> findAllQuestions(Page<Question> page);

    // 8. 根据题目类型查询题目列表
    @Select("SELECT * FROM questions WHERE question_type = #{questionType} AND del_flag = 0")
    List<Question> findByQuestionType(@Param("questionType") Integer questionType);

    // 9. 根据科目 ID 查询题目列表
    @Select("SELECT * FROM questions WHERE subject_id = #{subjectId} AND del_flag = 0")
    List<Question> findBySubjectId(@Param("subjectId") Integer subjectId);

    // 10. 根据难度级别查询题目列表
    @Select("SELECT * FROM questions WHERE difficulty = #{difficulty} AND del_flag = 0")
    List<Question> findByDifficulty(@Param("difficulty") Byte difficulty);

    // 11. 根据关键字模糊查询题目列表
    @Select("SELECT * FROM questions WHERE stem LIKE CONCAT('%', #{keyword}, '%') AND del_flag = 0")
    List<Question> findByKeyword(@Param("keyword") String keyword);

    // 12. 根据题目类型和科目 ID 查询题目列表
    @Select("SELECT * FROM questions WHERE question_type = #{questionType} AND subject_id = #{subjectId} AND del_flag = 0")
    List<Question> findByQuestionTypeAndSubjectId(@Param("questionType") Integer questionType, @Param("subjectId") Integer subjectId);

    // 13. 查询所有已删除的题目
    @Select("SELECT * FROM questions WHERE del_flag = 1")
    List<Question> findAllDeletedQuestions();

    // 14. 批量插入题目
    @Insert({
            "<script>",
            "INSERT INTO questions (stem, question_type, answer, options, difficulty, subject_id, analysis, del_flag, create_time) VALUES ",
            "<foreach collection='list' item='item' separator=','>",
            "(#{item.stem}, #{item.question_type}, #{item.answer}, #{item.options}, #{item.difficulty}, #{item.subject_id}, #{item.analysis}, #{item.delFlag}, #{item.createTime})",
            "</foreach>",
            "</script>"
    })
    int insertBatch(@Param("list") List<Question> questions);
    // 15. 查找重复的题目（根据 stem 字段）
    @Select("SELECT stem FROM questions WHERE del_flag = 0 GROUP BY stem HAVING COUNT(*) > 1")
    List<String> findDuplicateStems();

    // 16. 根据 stem 删除重复的题目（保留一个）
    @Delete("DELETE FROM questions WHERE id NOT IN (SELECT MIN(id) FROM questions WHERE stem = #{stem} AND del_flag = 0 GROUP BY stem) AND stem = #{stem}")
    int deleteDuplicatesByStem(@Param("stem") String stem);
}